package huoye.shops.huoye.user.presenter;


import android.app.Activity;
import android.os.CountDownTimer;
import android.util.Log;

import com.codeideology.android.log.LogUtils;
import com.codeideology.android.rpc.BaseJsonRequestCallback;
import com.codeideology.android.utils.StringUtils;

import huoye.shops.MainActivity;
import huoye.shops.R;
import huoye.shops.base.BasePresenter;
import huoye.shops.base.MineBaseJsonRequestCallback;
import huoye.shops.base.UserManager;
import huoye.shops.base.ZybCallBack;
import huoye.shops.common.model.SMSCoder;
import huoye.shops.huoye.mine.model.ShopDetials;
import huoye.shops.huoye.operation.model.OrderInfo;
import huoye.shops.huoye.user.activity.LoginActivity;
import huoye.shops.huoye.user.model.UpdateInfo;
import huoye.shops.huoye.user.model.User;
import huoye.shops.huoye.user.network.UserService;
import huoye.shops.huoye.user.view.ILoginView;
import okhttp3.Call;


/**
 * Created by uesr on 2017/7/19.
 */

public class LoginPresenter extends BasePresenter<ILoginView> {

    private UserService mService;
    private MyCountDownTimer myCountDownTimer;


   public void login(String mobile, String smsId,String smsCode) {
       if ( mobile.equals("") || smsCode.equals("")){
           view.makeToast(R.string.login_judge);
           return;
       }else {
          // view.showProcessingIndicator();
           mService.login(mobile,smsId,smsCode, new MineBaseJsonRequestCallback<User>(User.class) {

               //请求成功
               @Override
               public void onSuccess(User user) {
                   super.onSuccess(user);
                   if(user.getUserImg().equals("")){
                        user.setUserImg("1/shop/3/info/120712182229.jpg");
                   }
                   UserManager.getInstance().setUser(user);
                   view.startActivity(MainActivity.class);
                   view.finish();

               }

               //网络请求失败
               @Override
               public void onFailure(String msg, String code, Throwable e) {
                   super.onFailure(msg, code, e);
                   view.makeToast(msg);
               }

               //加载失败
               @Override
               public void onNetworkFail() {
                   super.onNetworkFail();
                   view.onNetworkFail();
               }

               //加载结束
               @Override
               public void onFinish() {
                   super.onFinish();
                   view.hideProcessingIndicator();
               }

           });
       }
    }





    //发送验证码
    public void senCode(String userTel){
       if(!StringUtils.isNotEmpty(userTel)){
           view.makeToast("请输入正确的手机号码！");
           return;
       }

        //   view.showProcessingIndicator();
        mService.sendRestPasswordSms(userTel,new MineBaseJsonRequestCallback<SMSCoder>(SMSCoder.class) {

            @Override
            public void onSuccess(SMSCoder smsCoder) {
                super.onSuccess(smsCoder);
                view.getCodeData(smsCoder);
                view.makeToast(R.string.login_code_confirm);
                myCountDownTimer.start();

            }

            //网络请求失败
            @Override
            public void onFailure(String msg, String code, Throwable e) {
                super.onFailure(msg, code, e);
                view.makeToast(msg);
                //   Log.e("testaaa",msg);
            }

            //加载结束
            @Override
            public void onNetworkFail() {
                super.onNetworkFail();
                view.onNetworkFail();
            }

            //加载中
            @Override
            public void onFinish() {
                super.onFinish();
                view.hideProcessingIndicator();
            }
        });
    }










    //检测升级
    public void checkUpdate() {
            mService.checkUpdate(new MineBaseJsonRequestCallback<UpdateInfo>(UpdateInfo.class) {

                //请求成功
                @Override
                public void onSuccess(UpdateInfo updateInfo) {
                    super.onSuccess(updateInfo);
                    view.onCheckUpdate(updateInfo);
                }

                //网络请求失败
                @Override
                public void onFailure(String msg, String code, Throwable e) {
                    super.onFailure(msg, code, e);
                    view.makeToast(msg);
                }

                //加载失败
                @Override
                public void onNetworkFail() {
                    super.onNetworkFail();
                    view.onNetworkFail();
                }

                //加载结束
                @Override
                public void onFinish() {
                    super.onFinish();
                    view.hideProcessingIndicator();
                }

            });


    }














    /**
     * 检查升级
     *

     */
  /*  public void getHelpList(final OnHelpListListener listener) {
        if (UserManager.getManager().getUser() != null) {
            String timestamp = String.valueOf(TimeUtil.getCurrentMillis());
            HttpManager.getManager().getService().getHelpList(timestamp, CommUtil.getSign(Constant.HELP_LIST, timestamp),
                    UserManager.getManager().getUser().user_token)
                    .enqueue(new Callback<HelpCenterInfo>() {
                        @Override
                        public void onResponse(Call<HelpCenterInfo> call, Response<HelpCenterInfo> response) {
                            if (response.body() != null && response.body().status == 200) {
                                if (response.body().data != null) {
                                    listener.onHelpListSuccess(response.body());
                                } else {
                                    listener.onHelpListFailed(response.body().info);
                                }
                            } else {
                                listener.onNetworkError();
                            }
                        }

                        @Override
                        public void onFailure(Call<HelpCenterInfo> call, Throwable t) {
                            listener.onNetworkError();
                        }
                    });
        }
    }
*/

    @Override
    public void onStart() {
        mService = new UserService();

        //new倒计时对象,总共的时间,每隔多少秒更新一次时间
        myCountDownTimer = new MyCountDownTimer(60000,1000);

    }

    //复写倒计时
    private class MyCountDownTimer extends CountDownTimer {

        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        //计时过程
        @Override
        public void onTick(long l) {
            //防止计时过程中重复点击
            view.codeByoyomi(l/1000+"s",false);

        }

        //计时完毕的方法
        @Override
        public void onFinish() {
            view.codeByoyomi("重新获取",true);
        }
    }


}
